import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_plugin_print_a300/flutter_plugin_print_a300.dart';
import 'package:flutter_print/permission_manager.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home:  MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  //打印插件实例
  final _flutterPluginPrintA300Plugin = FlutterPluginPrintA300();

  //打印数据, 对应key，传需要打印的数据
  Map<String, String> dataMap = {
    "brand":"B",
    "prefix":"16-21",
    "suffix":"8090",
    "shop": "布莱特玩具专营店", //店名
    "package": "盒装-A02黑粉蓝银", //包装
    "barcode": "12345678901234", //条码
    "qrcode": "1689769076,260028697616,YT3735281596253" //二维码
  };

  //申请权限后 打印
  void _testPrint() async {
    await _flutterPluginPrintA300Plugin.nativePrint(dataMap, null);
  }

  ///先申请权限
  Future<void> requestPermission() async {
    PermissionMan.requestBlePermissions().then(
            (value) => {
          if (value) {
            print("有权限"),
             _testPrint()
          } else {
            print("权限申请失败，请到设置界面 开启存储权限"),
          }
        }

    );

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
             Text(dataMap.toString()),
            const SizedBox(height: 100, width: 100,),
            const Text('点击打印按钮，打印上面数据',),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: requestPermission,
        tooltip: 'Increment',
        child: const Icon(Icons.print),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
